<template>
  <div class="callback">Loading... {{this.code}}</div>
</template>

<script>
import { mapGetters } from "vuex";
import contentApi from "@/api/api_content";
import businessApi from "@/api/api_business";

import { setStorage } from '@/utils/index.js';
	import { setToken } from '@/utils/auth.js';

export default {
  name: 'Callback',
  props: {
    code: {
      type: String
    },
           
  },
  mounted() {
    // 要先调另一个授权，获取token
      let paramsAuthToken = {
                            username: 'admin',
                            password: 'MyOIDC-2017',
                            grant_type: "password",
                            scope:'openid',
                            client_id: 'jupyterhub',
                            client_secret: 'jupyterhub',
                            response_type: 'code'
                        }
                        businessApi.oauthApi(paramsAuthToken).then((res) => {
                          if (res.data.error === 'invalid_grant') {
                              this.$message({
                                  message: res.data.error_description,
                                  type: 'error'
                              });
                          } else {
                              // window.sessionStorage.setItem('access-token', res.data.access_token)
                              setToken(res.data.access_token);
                              setStorage('baseInfo', { name: 'token', content: res.data.access_token, type: 'session', userName: res.data.account });
                          }
                        })
                        
    let v_code = this.$route.query.code
    console.log('code ------- ' + v_code)
    /*
    this.$store.dispatch("auth/signinRedirectCallback")
    //.then(this.$router.push("/user"));
    .then(function(){
      

      let paramsAuthorize = {
                            redirect_uri: '/user/admin/oauth_callback',
                            client_id: 'jupyterhub-user-admin',
                            client_secret: 'jupyterhub-user-admin',
                            grant_type: 'authorization_code',
                            code: v_code
                        }
                        contentApi.oauth2Api(paramsAuthorize).then((res) => {
                            console.log(JSON.stringify(res))
                            if (res.data.error === 'invalid_grant') {
                                this.$message({
                                    message: res.data.error_description,
                                    type: 'error'
                                });
                            } else {
                                
                            }
                        })
    })
    */
  },
    computed: {
    ...mapGetters({
        tokenResponse : 'auth/tokenResponse'
      })
  }
}
</script>